package com.sinoglobal.ningxia.fragment;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.MyCollectionActivity;
import com.sinoglobal.ningxia.activity.amusement.BusinessDetailActivity;
import com.sinoglobal.ningxia.adapter.MyCollectionAdapter;
import com.sinoglobal.ningxia.beans.MyCollectionDataVo;
import com.sinoglobal.ningxia.beans.MyCollectionItemVo;
import com.sinoglobal.ningxia.beans.MyCollectionRefreshVo;
import com.sinoglobal.ningxia.dialog.DialogBuilder;
import com.sinoglobal.ningxia.dialog.MDialogMethod;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.thread.ItktOtherAsyncTask;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.ToastBuilder;

import java.util.ArrayList;

/**
 * 
 * @author wxy
 * @createdate 2014-6-17 上午9:22:00
 * @Description: 我的收藏--美食
 */
public class MyCollectionCateFragment extends Fragment {

	private final int TAB_CATE = 0;// 代表美食，该字段用来做标签的切换，用来做handler的msg
	private View view;// 整个美食界面的view
	private ArrayList<MyCollectionItemVo> myCollectionDataVo = new ArrayList<MyCollectionItemVo>();// 用来显示美食列表的data
	private ListView myCollectionCateListView;// 该界面的listview
	private MyCollectionAdapter myCollectionAdapter;// 显示改界面的listview的adapter
	public static Handler myCollectionCateFragmentHandler;// 接收来自我的收藏界面中从服务器获取的消息
	private ProgressDialog pd;
	private TextView myCollectionNoDataShow;// 当该收藏为空的时候，将这个textview提示给用户

	public static Handler handler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Auto-generated method stub
		view = LayoutInflater.from(getActivity()).inflate(R.layout.my_collection_cate, null);
		init();// 初始化控件
		addListener();// 添加点击事件
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:// 刷新该收藏列表
					loadCollectionList();// 获取收藏列表
					break;

				default:
					break;
				}
			}
		};
		return view;
	}

	private void addListener() {
		// 长按删除该条收藏
		myCollectionCateListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {

				final DialogBuilder dialogBuilder = new DialogBuilder(getActivity());
				// 选择dialogBuilder中有两个button的构造方法
				dialogBuilder.Dialogbtn2(getString(R.string.sure_to_del_collect), R.layout.dialog_two_button, new MDialogMethod() {

					@Override
					public void dialogOk() {
						// 向服务器告知所删除的item
						new ItktOtherAsyncTask<Void, Void, MyCollectionRefreshVo>(true) {

							@Override
							public MyCollectionRefreshVo before(Void... params) throws Exception {
								// “type” //收藏类型 1娱乐 2美食 3新闻 4栏目 5安居
								// ‘action’:1|2 1代表添加收藏2代表删除收藏
								return RemoteImpl.getInstance().deleteMyCollection(FlyApplication.userId, "2", "2",
										myCollectionDataVo.get(position).getId());
							}

							@Override
							public void after(MyCollectionRefreshVo result) {
								if (result != null) {
									if (Code.CODE_SUCCESS != result.getCode()) {
										ToastBuilder.getIntance(FlyApplication.context).showToast(result.getMessage());
										dialogBuilder.mdialog.dismiss();
									} else {
										ToastBuilder.getIntance(FlyApplication.context).showToast(result.getMessage());
										FlyApplication.myCollectionTabCateNum = result.getCounts();
										if (MyCollectionActivity.myCollectionTabTextNumHandler != null) {
											MyCollectionActivity.myCollectionTabTextNumHandler.sendEmptyMessage(TAB_CATE);
										}
										myCollectionDataVo.remove(position);
										myCollectionAdapter.notifyDataSetChanged();
										dialogBuilder.mdialog.dismiss();
									}
								}
							}

							@Override
							public void exception() {
								// Auto-generated method stub
							}
						}.execute();
					}

					@Override
					public void dialogCancel() {
						dialogBuilder.mdialog.dismiss();
					}
				});

				return true;
			}
		});
		// 点击某一项查看收藏的详情
		myCollectionCateListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
				Intent intent = new Intent(getActivity(), BusinessDetailActivity.class);
				intent.putExtra(FlyApplication.isFromCollection, "1");
				intent.putExtra(BusinessDetailActivity.SHOPID, myCollectionDataVo.get(position).getId());
				intent.putExtra(BusinessDetailActivity.SHOPNAME, myCollectionDataVo.get(position).getShop_name());
				intent.putExtra(BusinessDetailActivity.TYPEID, "2");
				FlyUtil.intentForward(getActivity(), intent);
				getActivity().finish();
			}
		});
	}

	@SuppressLint("HandlerLeak")
	private void init() {
		myCollectionNoDataShow = (TextView) view.findViewById(R.id.my_collection_no_data_show);
		// 显示美食列表
		myCollectionCateListView = (ListView) view.findViewById(R.id.my_collection_cate_listView);
		if (null != myCollectionAdapter) {
			myCollectionCateListView.setAdapter(myCollectionAdapter);
			return;
		}
		loadCollectionList();// 获取收藏列表
		myCollectionCateFragmentHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case TAB_CATE:
					ItktLog.e("MyCollectionCateFragment=========handler");
					break;

				default:
					break;
				}
			}
		};
	}

	// 获取收藏列表
	private void loadCollectionList() {
		myCollectionDataVo.clear();
		new AsyncTask<Void, Void, MyCollectionDataVo>() {

			protected void onPreExecute() {
				pd = new ProgressDialog(getActivity());
				// pd.show();
			};

			@Override
			protected MyCollectionDataVo doInBackground(Void... params) {
				try {
					return RemoteImpl.getInstance().getMyCollectionDataVo(FlyApplication.userId, "2", "0", "10");
				} catch (Exception e) {
					// Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}

			protected void onPostExecute(MyCollectionDataVo result) {
				pd.dismiss();
				if (result != null) {
					if (Code.CODE_SUCCESS != result.getCode()) {
						myCollectionNoDataShow.setVisibility(View.VISIBLE);
					} else {
						if (result.getList().size() == 0) {
							myCollectionNoDataShow.setVisibility(View.VISIBLE);
						} else {
							myCollectionNoDataShow.setVisibility(View.GONE);
							ItktLog.i("MyCollectionCateFragment=========呗初始化了");
							myCollectionDataVo = result.getList();
							FlyApplication.myCollectionTabCateNum = result.getCol_num();
							myCollectionAdapter = new MyCollectionAdapter(myCollectionDataVo, getActivity(), "2");
							myCollectionCateListView.setAdapter(myCollectionAdapter);
						}
					}
				} else {
					myCollectionNoDataShow.setVisibility(View.VISIBLE);
				}
			};
		}.execute();
	}
}
